﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Meeting.Public;
using Newtonsoft.Json;

namespace Meeting.Web.Admin.MeetingSet
{
    /// <summary>
    /// MeetingOperation 的摘要说明
    /// </summary>
    public class MeetingOperation : IHttpHandler
    {
        string methodName = "";
        int nPageIndex = 1;
        int nPageSize = 4;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json;charset=utf-8";

            nPageIndex = CommonFunction.varToIntDef(context.Request.Params["page"], 1);
            nPageSize = CommonFunction.varToIntDef(context.Request.Params["rows"], 15);
            methodName = context.Request.Params["method"];
            switch (methodName)
            {
                case "MeetingInfoAdd":
                    MeetingInfoAdd(context);
                    break;
                case "SaveMeetingEmployee":
                    SaveMeetingEmployee(context);
                    break;

                case "GetPassTopic": //获取已经通过或部分通过的选题
                    GetTopic(context);
                    break;
                case "GetTopicApprove":
                    GetTopicApprove(context);
                    break;
                case "GetSelectTopic":
                    GetSelectTopic(context);
                    break;
                case "GetUseTopic":
                    GetUseTopic(context);
                    break;
                case "GetCategory":
                    GetCategory(context);
                    break;
                case "GetRoom":
                    GetRoom(context);
                    break;
                case "GetVideoRoom":
                    GetVideoRoom(context);
                    break;
                case "GetMeetingRoom":
                    GetMeetingRoom(context);
                    break;
                case "GetStandMeeting":
                    GetStandMeeting(context);
                    break;
                case "SaveMeetingTopic":
                    SaveMeetingTopic(context);
                    break;
                case "GetAllMeeting": //会议管理
                    context.Response.Write(GetAllMeeting(context));
                    break;
                case "AlterStatus":
                    AlterStatus(context);
                    break;
                case "GetMeetingTopicDocument":
                    GetMeetingTopicDocument(context);
                    break;
                case "DeleteMeetingTopicDocument":
                    DeleteMeetingTopicDocument(context);
                    break;
                //地块信息
                case "GetMeetingTopicPlate":
                    GetMeetingTopicPlate(context);
                    break;
                case "DeleteMeetingTopicPlate":
                    DeleteMeetingTopicPlate(context);
                    break;
                case "GetSelectVote":
                    GetSelectVote(context);
                    break;
                case "GetVote":
                    GetVote(context);
                    break;
                case "SaveMeetingVote":
                    SaveMeetingVote(context);
                    break;
                case "ActiveMeeting":
                    ActiveMeeting(context);
                    break;
            }
        }
        //获取我需要参加的会议
        private string GetAllMeeting(HttpContext context)
        {
            //throw new NotImplementedException();
            string Name = CommonFunction.removeAttackOfSql(context.Request.Params["Name"]);
            string Host = CommonFunction.removeAttackOfSql(CommonFunction.VarToStr(context.Request.Params["Host"]));
            string Begin = context.Request.Params["Begin"];
            string End = context.Request.Params["End"];

            string sF = " 1=1 ";
            if (!string.IsNullOrEmpty(Name)) sF += " And  Name Like '%" + Name + "%' ";
            if (!string.IsNullOrEmpty(Host)) sF += " And  HostName Like '%" + Host + "%' ";
            if (!string.IsNullOrEmpty(Begin)) sF += " And StartDate >= '" + Begin + "' ";
            if (!string.IsNullOrEmpty(End)) sF += " And  StartDate <= '" + End + "' ";
            BLL.MyMeeting rss = new BLL.MyMeeting();
            return rss.GetAllMeeting(nPageIndex, nPageSize, sF);
        }

        //读取常设会议人员
        private void GetStandMeeting(HttpContext context)
        {
            string CategoryId = context.Request["CategoryId"];
            BLL.StandMeeting bll = new BLL.StandMeeting();
            context.Response.Write(bll.GetMeetingById("", CategoryId)); //或常设会议信息
        }

        //获取Room信息ByRoomId
        private void GetMeetingRoom(HttpContext context)
        {
            int roomID = CommonFunction.varToIntDef(context.Request["RoomId"], 0);
            BLL.Room bll = new BLL.Room();
            context.Response.Write(bll.GetBy(roomID));
        }
        //获取会议类型
        private void GetCategory(HttpContext context)
        {
            BLL.MeetingExt bll = new BLL.MeetingExt();
            context.Response.Write(bll.GetCategoryJson());
        }
        //获取会议室
        private void GetRoom(HttpContext context)
        {
            BLL.MeetingExt bll = new BLL.MeetingExt();
            context.Response.Write(bll.GetRoomJson());
        }
        public void GetVideoRoom(HttpContext context)
        {
            BLL.MeetingExt bll = new BLL.MeetingExt();
            context.Response.Write(bll.GetVideoRoomJson());
        }
        //保存会议基本信息
        private void MeetingInfoAdd(HttpContext context)
        {
            Model.Meeting model = new Model.Meeting();
            model.Id = CommonFunction.varToIntDef(context.Request.Params["Id"], 0);
            model.Name = CommonFunction.removeAttackOfSql(context.Request.Params["Name"]);
            model.HostId = CommonFunction.VarToStr(context.Request.Params["HostId"]);
            model.HostName = CommonFunction.VarToStr(context.Request.Params["HostName"]);
            model.HosetRoleId = CommonFunction.VarToStr(context.Request.Params["HosetRoleId"]);
            model.StartDate = CommonFunction.varToDateTimeDef(context.Request.Params["StartDate"], DateTime.Now);
            model.EndDate = CommonFunction.varToDateTimeDef(context.Request.Params["EndDate"], DateTime.Now);
            //
            model.EmployeeId = UserInfo.LoginName;
            model.EmployeeName = UserInfo.UserName;
            model.EmployeeTitle = UserInfo.Title;
            model.RoleId = UserInfo.RoleId;
            //
            model.RoomId = CommonFunction.varToIntDef(context.Request.Params["RoomId"], 0);
            string temp = CommonFunction.VarToStr(context.Request.Params["VideoRoomId"]);
            if (temp != "")
                model.VideoRoomId = Guid.Parse(temp);
            model.Duration = Convert.ToDecimal(context.Request.Params["Duration"]);
            model.Status = CommonFunction.varToIntDef(context.Request.Params["Status"], 0);
            model.CategoryId = CommonFunction.varToIntDef(context.Request.Params["CategoryId"], 0);

            //
            Model.JsonResponse res = new Model.JsonResponse();
            BLL.MeetingExt bll = new BLL.MeetingExt();
            int nId = bll.MeetingInfoAdd(model);
            if (nId > 0)
            {
                res.success = true;
                res.Message = nId.ToString();
            }
            else
            {
                res.success = false;
                res.Message = bll.errorString;
            }
            context.Response.Write(JsonConvert.SerializeObject(res));
        }

        // 保存议题
        private void SaveMeetingTopic(HttpContext context)
        {
            string strJson = context.Request["data"];
            string meetingId = context.Request["Id"];
            Model.JsonResponse res = new Model.JsonResponse();
            BLL.MeetingExt bll = new BLL.MeetingExt();
            //
            try
            {
                if (bll.SaveMeetingTopic(meetingId, strJson))
                {
                    res.success = true;
                }
                else
                {
                    res.success = false;
                    res.Message = bll.errorString;
                }
            }
            catch (Exception ex)
            {
                res.success = false;
                res.Message = ex.Message;
            }
            context.Response.Write(JsonConvert.SerializeObject(res));
        }
        //参会人员
        private void SaveMeetingEmployee(HttpContext context)
        {
            //会议ID
            string meetingId = CommonFunction.tryNullToString(context.Request.Params["Id"], "");
            string Host = context.Request.Params["Host"];
            string HostId = context.Request.Params["HostId"];
            string RoleId = context.Request.Params["HostRoleId"];
            string HostTitle = context.Request.Params["HostTitle"];
            //用户ID
            string selEmployee = context.Request.Params["selEmployee"];
            //
            Model.JsonResponse res = new Model.JsonResponse();
            BLL.MeetingExt bll = new BLL.MeetingExt();
            if (bll.SaveMeetingEmployee(meetingId, Host, HostId, RoleId, HostTitle, selEmployee))
            {
                res.success = true;
            }
            else
            {
                res.success = false;
                res.Message = bll.errorString;
            }
            context.Response.Write(JsonConvert.SerializeObject(res));
        }
        //更改会议状态:
        private void AlterStatus(HttpContext context)
        {
            string Id = context.Request["Id"];
            string Status = context.Request["Status"];
            BLL.MeetingExt bll = new BLL.MeetingExt();
            Model.JsonResponse rs = new Model.JsonResponse();
            rs.success = bll.AlterStatus(Id, Status);
            rs.Message = bll.errorString;
            context.Response.Write(JsonConvert.SerializeObject(rs));
        }

        //读取已经选择的议题
        private void GetSelectTopic(HttpContext context)
        {
            string meetingId = CommonFunction.tryNullToString(context.Request.Params["Id"], "");
            BLL.TopicExt bll = new BLL.TopicExt();
            context.Response.Write(bll.GetSelectTopic(meetingId));
        }
        //获取已选议题的相关文档
        private void GetMeetingTopicDocument(HttpContext context)
        {
            string topicId = context.Request.Params["Id"];
            BLL.TopicExt bll = new BLL.TopicExt();
            context.Response.Write(bll.GetMeetingTopicDocument(topicId));
        }
        //删除议题文档的关联
        private void DeleteMeetingTopicDocument(HttpContext context)
        {
            string MeetingId = context.Request["MeetingId"];
            string TopicId = context.Request["TopicId"];
            string DocumentId = context.Request["DocumentId"];
            BLL.TopicExt bll = new BLL.TopicExt();
            Model.JsonResponse rs = new Model.JsonResponse();
            rs.success = bll.DeleteMeetingTopicDocument(MeetingId, TopicId, DocumentId);
            rs.Message = bll.errorString;
            context.Response.Write(JsonConvert.SerializeObject(rs));
        }

        //获取地块信息 
        private void GetMeetingTopicPlate(HttpContext context)
        {
            string topicId = context.Request.Params["Id"];
            BLL.TopicExt bll = new BLL.TopicExt();
            context.Response.Write(bll.GetMeetingTopicPlate(topicId));
        }
        private void DeleteMeetingTopicPlate(HttpContext context)
        {
            string MeetingId = context.Request["MeetingId"];
            string TopicId = context.Request["TopicId"];
            string PlateId = context.Request["PlateId"];
            BLL.TopicExt bll = new BLL.TopicExt();
            Model.JsonResponse rs = new Model.JsonResponse();
            rs.success = bll.DeleteMeetingTopicPlate(MeetingId, TopicId, PlateId);
            rs.Message = bll.errorString;
            context.Response.Write(JsonConvert.SerializeObject(rs));

        }
        //
        private void GetUseTopic(HttpContext context)
        {
            string CategoryId = context.Request.Params["CategoryId"];
            string Title = CommonFunction.removeAttackOfSql(context.Request.Params["Title"]);
            string sf = " Title Like '%" + Title + "%' ";
            BLL.TopicExt bll = new BLL.TopicExt();
            context.Response.Write(bll.GetTopic(nPageIndex, nPageSize, CategoryId, sf, true));
        }
        //读取通过的会议议题
        private void GetTopic(HttpContext context)
        {
            string CategoryId = context.Request.Params["CategoryId"];
            string Title = CommonFunction.removeAttackOfSql(context.Request.Params["Title"]);
            string sf = "";
            if (Title != "")
                sf = " Title Like '" + Title + "%' ";
            BLL.TopicExt bll = new BLL.TopicExt();
            context.Response.Write(bll.GetTopic(nPageIndex, nPageSize, CategoryId, sf, false));
        }
        //获取会议明细
        private void GetTopicApprove(HttpContext context)
        {
            string Id = context.Request.Params["Id"];
            BLL.TopicExt bll = new BLL.TopicExt();
            context.Response.Write(bll.GetTopicDetail(Id));
        }

        //获取参会人员议题
        private string GetEmployeeTopic(HttpContext context)
        {
            string meetId = context.Request["Id"];
            if (meetId == "") return "";
            return "";

        }
        #region 投票设置
        private void GetSelectVote(HttpContext context)
        {
            string meetingId = context.Request["Id"];
            BLL.MeetingExt bll = new BLL.MeetingExt();
            context.Response.Write(bll.GetSelectVote(meetingId));
        }
        //获取设置好的投票项目
        private void GetVote(HttpContext context)
        {
            BLL.Vote bll = new BLL.Vote();
            string Theme = CommonFunction.removeAttackOfSql(context.Request.Params["Theme"]);
            string sf = " Theme Like '%" + Theme + "%' ";
            context.Response.Write(bll.GetPage(nPageIndex, nPageSize, sf));
        }
        // 保存议题
        private void SaveMeetingVote(HttpContext context)
        {
            string strJson = context.Request["data"];
            string meetingId = context.Request["Id"];
            Model.JsonResponse res = new Model.JsonResponse();
            BLL.MeetingExt bll = new BLL.MeetingExt();
            //
            if (bll.SaveMeetingVote(meetingId, strJson))
            {
                res.success = true;
            }
            else
            {
                res.success = false;
                res.Message = bll.errorString;
            }
            context.Response.Write(JsonConvert.SerializeObject(res));
        }
        #endregion

        //激活会议
        private void ActiveMeeting(HttpContext context)
        {
            string meetingId = context.Request["Id"];
            Model.JsonResponse res = new Model.JsonResponse();
            BLL.MeetingExt bll = new BLL.MeetingExt();
            //
            if (bll.AlterStatus(meetingId, "2"))
            {
                res.success = true;
            }
            else
            {
                res.success = false;
                res.Message = bll.errorString;
            }
            context.Response.Write(JsonConvert.SerializeObject(res));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}